package com.lankystar.logistucs.spring.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lankystar.logistucs.spring.dao.IAddressDao;
import com.lankystar.logistucs.spring.dao.ICargoDao;
import com.lankystar.logistucs.spring.dao.ICompanyDao;
import com.lankystar.logistucs.spring.dao.IOneOrderAndOneStatusDao;
import com.lankystar.logistucs.spring.dao.IOneOrderDao;
import com.lankystar.logistucs.spring.dao.IOrderStatusDao;
import com.lankystar.logistucs.spring.dao.IOrderTypeDao;
import com.lankystar.logistucs.spring.dao.IRecordDao;
import com.lankystar.logistucs.spring.dao.ITruckDao;
import com.lankystar.logistucs.spring.dao.ITruckStatusDao;
import com.lankystar.logistucs.spring.dao.ITwoOrderAndLastStatusDao;
import com.lankystar.logistucs.spring.dao.ITwoOrderAndTwoStatusDao;
import com.lankystar.logistucs.spring.dao.ITwoOrderDao;
import com.lankystar.logistucs.spring.dao.IUserDao;
import com.lankystar.logistucs.spring.entity.Address;
import com.lankystar.logistucs.spring.entity.Cargo;
import com.lankystar.logistucs.spring.entity.Company;
import com.lankystar.logistucs.spring.entity.LastStatus;
import com.lankystar.logistucs.spring.entity.OneOrder;
import com.lankystar.logistucs.spring.entity.OneOrderAndOneStatus;
import com.lankystar.logistucs.spring.entity.OneStatus;
import com.lankystar.logistucs.spring.entity.OrderType;
import com.lankystar.logistucs.spring.entity.Record;
import com.lankystar.logistucs.spring.entity.Truck;
import com.lankystar.logistucs.spring.entity.TruckStatus;
import com.lankystar.logistucs.spring.entity.TwoOrder;
import com.lankystar.logistucs.spring.entity.TwoOrderAndLastStatus;
import com.lankystar.logistucs.spring.entity.TwoOrderAndTwoStatus;
import com.lankystar.logistucs.spring.entity.TwoStatus;
import com.lankystar.logistucs.spring.entity.User;
import com.lankystar.logistucs.spring.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService{

	@Autowired
	private IOneOrderDao oneOrderDao;
	@Autowired
	private ITwoOrderDao twoOrderDao;
	@Autowired
	private ICargoDao cargoDao;
	@Autowired
	private IAddressDao addressDao;
	@Autowired
	private ICompanyDao companyDao;
	@Autowired
	private IUserDao userDao;
	@Autowired
	private ITruckDao truckDao;
	@Autowired
	private IRecordDao recordDao;
	@Autowired
	private ITruckStatusDao truckStatusDao;
	@Autowired
	private ITwoOrderAndTwoStatusDao tatDao;
	@Autowired
	private IOneOrderAndOneStatusDao oaoDao;
	@Autowired
	private IOrderStatusDao statusDao;
	@Autowired
	private IOrderTypeDao otDao;
	@Autowired
	private ITwoOrderAndLastStatusDao talDao;
	@Override
	public List<Map<String, Object>> addOneOrder(String values,Integer truckId,Integer userId,User user) {
		/**
		 * 苏州报关行  一次  只给一个   总提单号  分单号cargo只有一个
		 */
		List<Map<String, Object>> maplist = new ArrayList<>();
		Company company = companyDao.selectCompany(user.getUserId());
		Address address = addressDao.selectAddressById(1);//写死苏州报关行
		User u = userDao.selectUserById(userId);//司机
		Truck truck = truckDao.selectTruckById(truckId);
		
		/**
		 * 一级订单
		 */
		OneOrder oneOrder = new OneOrder();
		oneOrder.setAddress(address);
		oneOrder.setCompany(company);
		oneOrder.setType(1);
		oneOrder.setCreateTime(new Date().getTime());
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHH");
		String oneOrderNu ="KY"+sdf.format(new Date());
		oneOrder.setOneOrderNu(oneOrderNu);
		oneOrderDao.add(oneOrder);
		/**
		 * 出车记录
		 */
		Record record = new Record();
		record.setCreateTime(new Date().getTime());
		record.setOneOrder(oneOrder);
		recordDao.add(record);
		/**
		 * 生成二级订单
		 */
		TwoOrder twoOrder = new TwoOrder();
		twoOrder.setCreateTime(new Date().getTime());
		twoOrder.setOneOrder(oneOrder);
		twoOrder.setType(1);
		twoOrder.setTruck(truck);
		String twoOrderNu ="KYTWO"+sdf.format(new Date());
		twoOrder.setTwoOrderNu(twoOrderNu);
		twoOrder.setUser(u);
		twoOrder.setRemove(0);
		
		
		
		
		/**
		 * 把一级订单关联到货运单，并更新货运单
		 */
		String[] cargoIds = values.split(",");
		for (String cargoId : cargoIds) {
			Map<String, Object> map = new HashMap<>();
			Cargo cargo = cargoDao.selectCargoById(Integer.parseInt(cargoId));
			cargo.setOneOrder(oneOrder);
			cargoDao.update(cargo);
			/**
			 * 最后一公里,直接生成一级订单状态，不经过报关行
			 */
			if(cargo.getBroker()==null){
				OneOrderAndOneStatus oao = new OneOrderAndOneStatus();
				OrderType ot = otDao.selectOrderTypeById(2);
				/**
				 * 添加订单类型 短驳运输
				 */
				twoOrder.setOrderType(ot);
				twoOrderDao.add(twoOrder);
				OneStatus oneStatus = statusDao.selectOneStatusBykey(30);
				TwoOrderAndLastStatus tal = new TwoOrderAndLastStatus();
				LastStatus lastStatus = statusDao.selectLastStatusBykey(30); 
				tal.setTwoOrder(twoOrder);
				tal.setCreateTime(new Date().getTime());
				tal.setLastStatus(lastStatus);
				talDao.add(tal);
				oao.setOneOrder(oneOrder);
				oao.setOneStatus(oneStatus);
				oaoDao.add(oao);
			}else{
				twoOrderDao.add(twoOrder);
			}
			
			map.put("totalOrderNu", cargo.getTotalOrderNu());
			map.put("pointOrderNu", cargo.getPointOrderNu());
			map.put("cardNu", truck.getCardNu());
			maplist.add(map);
		}
		/////////////////////更新车状态、司机状态 (已安排)
		TruckStatus truckStatus = truckStatusDao.selectTruckStatusBykey(10);
		truck.setTruckStatus(truckStatus);
		u.setTruckStatus(truckStatus);
		truckDao.update(truck);
		userDao.update(u);
		return maplist;
	}
	
	@Override
	public Map<String, Object> updateOneOrder(User user, int oneStatusKey) {
		Object[] ob = oneOrderDao.selectOneOrderAndTwoOrder(user.getUserId());
		TwoOrder twoOrder = (TwoOrder) ob[0];
		OneOrder oneOrder = twoOrder.getOneOrder();
		Truck truck = (Truck) ob[2];
		OneOrderAndOneStatus oao = new OneOrderAndOneStatus();
		TwoOrderAndTwoStatus tat = new TwoOrderAndTwoStatus();
		TwoOrderAndLastStatus tal = new TwoOrderAndLastStatus();
		/**
		 * key值同等变化
		 */
		OneStatus oneStatus = statusDao.selectOneStatusBykey(oneStatusKey);
		
		
		
		User u = userDao.selectUserById(user.getUserId());
		
			if(oneStatusKey!=70&&oneStatusKey!=110){
				
				TruckStatus truckStatus = truckStatusDao.selectTruckStatusBykey(oneStatusKey);
				/**
				 * 更新司机、车辆状态
				 */
				truck.setTruckStatus(truckStatus);
				u.setTruckStatus(truckStatus);
				userDao.update(u);
				truckDao.update(truck);
			}
			if(oneStatusKey==110||oneStatusKey==70){
				TruckStatus truckStatus = truckStatusDao.selectTruckStatusBykey(1);
				/**
				 * 更新司机、车辆状态
				 */
				truck.setTruckStatus(truckStatus);
				truck.setAddress("SZ");
				u.setAddress("SZ");
				
				u.setTruckStatus(truckStatus);
				userDao.update(u);
				truckDao.update(truck);
				twoOrder.setType(0);
				oneOrder.setType(0);
				twoOrderDao.update(twoOrder);
				oneOrderDao.update(oneOrder);
			}
			/**
			 * 干线运输  与   短驳运输
			 */
		if(twoOrder.getOrderType()==null){
			TwoStatus twoStatus = statusDao.selectTwoStatusBykey(oneStatusKey);
			tat.setTwoOrder(twoOrder);
			tat.setTwoStatus(twoStatus);
			tat.setCreateTime(new Date().getTime());
			tatDao.add(tat);
		}else{
			LastStatus lastStatus = statusDao.selectLastStatusBykey(oneStatusKey);
			tal.setTwoOrder(twoOrder);
			tal.setLastStatus(lastStatus);
			tal.setCreateTime(new Date().getTime());
			talDao.add(tal);
		}
		
		oao.setOneOrder(oneOrder);
		oao.setOneStatus(oneStatus);
		oao.setCreateTime(new Date().getTime());
		oaoDao.add(oao);
		
		Map<String, Object> map = new HashMap<>();
		map.put("UserId", user.getUserId());
		map.put("OneOrderId", oneOrder.getOneOrderId());
		map.put("OneStatusKey", oneStatus.getOneStatusKey());
		map.put("OneStatusValue", oneStatus.getOneStatusValue());
		
		return map;
	}

}
